// Domob JavaScript Interface

var domobjs = window.domobjs = {};
mraidbridge = window.mraidbridge = {};

mraidbridge.executeNativeCall = function(command) {
	var call = 'domobjs://' + command;

	var key, value;
	var isFirstArgument = true;

	for ( var i = 1; i < arguments.length; i += 2) {
		key = arguments[i];
		value = arguments[i + 1];

		if (value === null) {
			continue;
		}

		if (isFirstArgument) {
			call += '?';
			isFirstArgument = false;
		} else {
			call += '&';
		}

		call += key + '=' + encodeURIComponent(value);
	}

	location.href = call;
};

domobjs.addAsset = function(alias, URL) {
	mraidbridge.executeNativeCall('addAsset', 'alias', alias, 'url', URL);
};

/**
 * 一次添加多个资源缓存
 */
domobjs.addAssets = function(imgs) {
	var call = 'domobjs://addAssets';
	var isFirstArgument = true;

	for (img in imgs) {
		var alias = img;
		var url = imgs[img];

		if (url === null) {
			continue;
		}

		if (isFirstArgument) {
			call += '?';
			isFirstArgument = false;
		} else {
			call += '&';
		}

		call += alias + '=' + encodeURIComponent(url);
	}

	location.href = call;
}

domobjs.getAsset = function(alias) {

};

domobjs.assetReady = function(alias, path) {
	var img = document.getElementById(alias);
	img.setAttribute("src", "file://" + path);
};

domobjs.removeAsset = function(alias) {
	mraidbridge.executeNativeCall('removeAsset', 'alias', alias);
};
